package com.openFire.Base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.openFire.Service.UserService;
import com.openFire.entity.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.schooldate.model.entity.*;
import com.schooldate.model.service.*;
import com.schooldate.util.CommomUtils;
import net.sf.json.JsonConfig;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by etund on 2015/3/12 0012.
 */
@SuppressWarnings("all")
public abstract class OpenFireBaseAction<T> extends ActionSupport implements ModelDriven<T>,SessionAware {

    /**
     * 文件上传
     */


    /**
     * 分页数据
     */
    protected Integer pageNo = 1;
    protected Integer totalCount = 0;
    protected Integer pageSize = 3;

    /**
     * entity类的集合
     */
     protected List<User> users ;


    /**
     * service类
     */
    @Autowired
    protected UserService userService;

    /**
     * 公共字段
     */
    protected String result;
    protected JSONObject object = new JSONObject();
    protected JsonConfig jsonConfig = new JsonConfig();
    protected String refreshNum;

    //    邮箱
    @Autowired
    protected JavaMailSenderImpl mailSender;


    protected  Map<String ,Object> session;
    protected   T model;
    public OpenFireBaseAction() {
        try {
            ParameterizedType pt = (ParameterizedType)this.getClass().getGenericSuperclass();
            Class<T> clazz = (Class<T>)pt.getActualTypeArguments()[0];
            model = clazz.newInstance();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //    以response方式输出json
    public void renderJson(String json) throws IOException {
        getResponse().setContentType("application/json;charset=UTF-8");
        getResponse().getWriter().write(json);
    }

//    邮件发送
    public  void sendEmail(String verificationCode, String email) throws Exception{
        try {
            MimeMessage mailMessage = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
            messageHelper.setFrom(mailSender.getUsername());
            messageHelper.setTo(email);
            messageHelper.setSubject("好久不见，这是校缘验证码");
//            ？register="+register
            String tits = "验证码将在一个小时候失效";
            tits = new String(tits.getBytes("iso-8859-1"),"utf-8");
            messageHelper.setText("<html><body>"+verificationCode+"</body></html>", true);
            Properties properties = new Properties();
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.timeout", "25000");
            mailSender.send(mailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ServletResponse getResponse(){
        return ServletActionContext.getResponse();
    }

    @Override
    public T getModel() {
        return model;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public String getRefreshNum() {
        return refreshNum;
    }

    public void setRefreshNum(String refreshNum) {
        this.refreshNum = refreshNum;
    }
}